package com.yunxi.service.tpf.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yunxi.core.base.Constant;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.eai.pojo.esb.EsbVO;
import com.yunxi.eai.pojo.tpf.OrderInfoSynVO;
import com.yunxi.eai.service.tpf.TpfService;
import com.yunxi.hubToBaseData.pojo.OrderInfoVO;
import com.yunxi.hubToBaseData.service.AppCustomerCredit2PMS;
import com.yunxi.model.eai.EaiLog;
import com.yunxi.rocketmq.producer.EaiLogsProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.json.Json;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by Administrator on 2019/9/16.
 */
@Service
@Qualifier("tpfServiceImpl")
public class TpfServiceImpl implements TpfService {
    private static final Logger logger = LoggerFactory.getLogger(TpfServiceImpl.class);
    @Autowired
    private EaiLogsProducer eaiLogsProducer;

    @Autowired
    private AppCustomerCredit2PMS appCustomerCredit2PMSImpl;

    @Override
    public ApiResult distributeTpf(EsbVO esbVO) {
        String serviceName = esbVO.getServiceName();
        String reqBody = esbVO.getReqBody();
        ApiResult apiResult = null;
        if (serviceName.equals(Constant.TPF_PMS_ORDER_INFO_SYN)) {
            //订单信息同步
            apiResult=saveOrderInfoSyn(esbVO);
        }
        else {
            apiResult= ResponseUtil.fail(500,"没有对应的业务接口!");
        }
        return apiResult;
    }

    private ApiResult saveOrderInfoSyn(EsbVO esbVO) {
        ApiResult apiResult = null;
        EaiLog eaiLog=new EaiLog();
        String reqBody = esbVO.getReqBody();
        eaiLog.setEsbEaiContent(esbVO.getReqBody());
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("第三方金融公司发送订单反馈信息给PMS");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setServiceName(esbVO.getServiceName());
        OrderInfoVO orderInfoVO= JSONObject.toJavaObject(JSON.parseObject(reqBody),OrderInfoVO.class);
        try {
            //内部业务
            apiResult = appCustomerCredit2PMSImpl.YCYJSyncOrderInfo2PMS(orderInfoVO);
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
            if (apiResult.getCode() == -1) {
                eaiLog.setDisposeStatus(2);
            }
        }catch (Exception e){
            apiResult=ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
            logger.error(e.getMessage(),e);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage(),e);
        }
        return apiResult;
    }
}
